package com.zhou.common.mybatis.template.shiro;

import org.flowable.engine.*;
import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.flowable.variable.api.history.HistoricVariableInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//@Component
public class BPMNCommandLineRunner implements CommandLineRunner {

    @Autowired
    private ProcessEngine processEngine;

    @Override
    public void run(String... args) throws Exception {
        TaskService taskService = processEngine.getTaskService();
        FormService formService = processEngine.getFormService();
        HistoryService historyService = processEngine.getHistoryService();

        RepositoryService repositoryService = processEngine.getRepositoryService();
        List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().active().list();
        list.forEach(System.out::println);

        List<ProcessDefinition> test1 = repositoryService.createProcessDefinitionQuery().processDefinitionKey("test1").latestVersion().list();

        RuntimeService runtimeService = processEngine.getRuntimeService();

        List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup("test1_group1").list();
        tasks.forEach(System.out::println);
        tasks = taskService.createTaskQuery().taskCandidateUser("test11").list();
        tasks.forEach(System.out::println);
        tasks = taskService.createTaskQuery().taskAssignee("xianfeng").list();
        tasks.forEach(System.out::println);

        Map<String, Object> variables = new HashMap<String, Object>();
        //设置审批人
        variables.put("test1", "test11");
        variables.put("test1_group", "test1_group1");
        ProcessInstance processInstance =
                runtimeService.startProcessInstanceByKey("test2", variables);
        System.out.println(processInstance);


        List<HistoricProcessInstance> list1 = historyService.createHistoricProcessInstanceQuery().list();
        System.out.println(list1);


        tasks = taskService.createTaskQuery().taskCandidateGroup("test1_group1").list();
        tasks.forEach(System.out::println);
        tasks = taskService.createTaskQuery().taskCandidateUser("test11").list();
        tasks.forEach(System.out::println);
        //领取任务
        Task task = tasks.get(0);
        taskService.claim(task.getId(), "test11");
        tasks = taskService.createTaskQuery().taskAssignee("test11").list();
        tasks.forEach(System.out::println);
//        Task task=tasks.get(0);
        //完成任务
        variables = new HashMap<String, Object>();
        //设置审批人
        variables.put("test2", "test22");
        variables.put("test2_group", "test2_group2");
//        taskService.complete(task.getId(),variables);
        tasks = taskService.createTaskQuery().taskCandidateGroup("test2_group2").list();
        tasks.forEach(System.out::println);
        tasks = taskService.createTaskQuery().taskCandidateUser("test22").list();
        tasks.forEach(System.out::println);

        task = tasks.get(0);
        taskService.claim(task.getId(), "test22");
        variables = new HashMap<String, Object>();
        //设置审批人
        variables.put("test3", "test33");
        variables.put("test3_group", "test3_group3");
        taskService.complete(task.getId(), variables);
        tasks = taskService.createTaskQuery().taskCandidateGroup("test3_group3").list();
        tasks.forEach(System.out::println);
        tasks = taskService.createTaskQuery().taskCandidateUser("test33").list();
        tasks.forEach(System.out::println);
        task = tasks.get(0);
        taskService.claim(task.getId(), "test33");
        taskService.complete(task.getId());



        /*String taskFormKey = formService.getTaskFormKey(tasks.get(0).getProcessDefinitionId(), tasks.get(0).getTaskDefinitionKey());
        TaskFormData taskFormData = formService.getTaskFormData(tasks.get(0).getId());
        System.out.println(taskFormKey);
        System.out.println(taskFormData);*/
        /*FormInfo taskFormModel = taskService.getTaskFormModel(tasks.get(0).getId());
        System.out.println(taskFormModel);
        Task task=tasks.get(0);
        taskService.completeTaskWithForm(task.getId(),taskFormModel.getId(),"outcome", Maps.newHashMap());*/


        // 验证流程已经结束
        String processInstanceId="83389ed3-bfe3-11ec-821f-5414f3dd8146";
        HistoricProcessInstance historicProcessInstance =
                historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
        System.out.println("Process instance end time: " + historicProcessInstance.getEndTime());

        //历史流程实例查询
        List<HistoricProcessInstance> historicProcessInstances = historyService.createHistoricProcessInstanceQuery()
                .finished()
                .processDefinitionId(processInstanceId)
                .orderByProcessInstanceDuration().desc()
                .list();
//                .listPage(0, 10);
        historicProcessInstances.forEach(System.out::println);

        List<HistoricVariableInstance> list2 = historyService.createHistoricVariableInstanceQuery()
                .processInstanceId(processInstanceId)
                .orderByVariableName().desc()
                .list();
        list2.forEach(System.out::println);

        List<HistoricActivityInstance> historicActivityInstances = historyService.createHistoricActivityInstanceQuery()
                .activityType("serviceTask")
                .processDefinitionId("XXX")
                .finished()
                .orderByHistoricActivityInstanceEndTime().desc()
                .listPage(0, 1);

        List<HistoricTaskInstance> historicTaskInstances = historyService.createHistoricTaskInstanceQuery()
                .finished()
                .processInstanceId(processInstanceId)
                .orderByHistoricTaskInstanceDuration().desc()
                .listPage(0, 10);
        historicTaskInstances.forEach(System.out::println);
    }
}
